package com.leyou.trade.web;

import com.leyou.trade.entity.Cart;
import com.leyou.trade.entity.CartItem;
import com.leyou.trade.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private MongoTemplate mongoTemplate;

    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody CartItem cartItem) {

        this.cartService.addCart(cartItem);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping("/list")
    public ResponseEntity<List<CartItem>> findSku(){

        return ResponseEntity.ok(this.cartService.findSku());
    }

    @PutMapping
    public ResponseEntity<Void> updateSku( @RequestParam("id") Long skuId, @RequestParam("num") Integer num){

        this.cartService.updateSku(skuId,num);
        return ResponseEntity.noContent().build();
    }

    @DeleteMapping
    public ResponseEntity<Void> deleteSku(@PathVariable("skuId") Long skuId){

        this.cartService.deleteSku(skuId);
        return ResponseEntity.noContent().build();
    }

    @PostMapping("/list")
    public ResponseEntity<Void> addCartItemList(@RequestBody List<CartItem> itemList){

        this.cartService.addCartItemList(itemList);
        return ResponseEntity.status(HttpStatus.CREATED).build();

    }
}
